package com.wude.guild.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mc.developmentkit.utils.MCUtils;
import com.wude.bean.CommentariesBean;
import com.wude.guild.GameDetActivity;
import com.wude.guild.R;

import java.util.List;

/**
 * Created by wude01 on 2017/10/27.
 */

public class CommentariesAdapter extends RecyclerView.Adapter<CommentariesAdapter.CommentriesItem> {
    private List<CommentariesBean> datas;
    private Context context;
    private LinearLayout content;

    public CommentariesAdapter(List<CommentariesBean> datas, Context context) {
        this.datas = datas;
        this.context = context;
    }

    @Override
    public CommentriesItem onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.commentaries_item, parent, false);
        return new CommentriesItem(view);
    }

    @Override
    public void onBindViewHolder(CommentriesItem holder, int position) {
        final CommentariesBean commentariesBean = datas.get(position);
        holder.tv_gameName.setText(commentariesBean.game_name);
        holder.tv_comment.setText(commentariesBean.comment);
        holder.tv_nickName.setText(commentariesBean.nickname);
        MCUtils.fillImage(holder.iv_gameIcon, commentariesBean.game_icon);

        content = holder.ll_content;
        holder.ll_content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, GameDetActivity.class);
                intent.putExtra("id", commentariesBean.game_id);
                intent.putExtra("type", "");
                intent.putExtra("comment", true);
                context.startActivity(intent);
            }
        });
    }

    public void setItemOnclick(boolean onclick) {
        if (onclick && content != null) {
            content.setClickable(true);
        } else if (!onclick && content != null) {
            content.setClickable(false);
        }

    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    public static class CommentriesItem extends RecyclerView.ViewHolder {
        TextView tv_gameName;
        TextView tv_comment;
        TextView tv_nickName;
        ImageView iv_gameIcon;
        LinearLayout ll_content;

        public CommentriesItem(View itemView) {
            super(itemView);
            tv_gameName = itemView.findViewById(R.id.tv_gameName);
            tv_comment = itemView.findViewById(R.id.tv_comment);
            tv_nickName = itemView.findViewById(R.id.tv_nickName);
            iv_gameIcon = itemView.findViewById(R.id.iv_gameIcon);
            ll_content = itemView.findViewById(R.id.ll_content);
        }
    }

    public void setDataList(List<CommentariesBean> dataList) {
        this.datas = dataList;
        notifyDataSetChanged();
    }
}
